var g_updateProxy = null;
var UpdateProxy = cc.Node.extend({
    _updateTowerFlag : null,
    //////////////////////////////
    _isShow  : false,
    //////////////////////////////
    _updateT : null,
    _removeT : null,
    //////////////////////////////
    _towerUpdate : null,
    //////////////////////////////
    _updateLabel : null,
    _removeLabel : null,
    ctor : function(){
        this._super();
        g_updateProxy=this;
        g_updateProxy.retain();
        this.addTowerFlag();
    },
    onEnter:function () {
        this._super();
    },
    onExit : function() {
        this._super();
    },
    addTowerFlag : function( ){
        var filename=MainResource("c0.png");
        this._updateTowerFlag = new cc.Sprite( filename );
        this._updateTowerFlag.setPosition( cc.p( cc.winSize.width*0.5,cc.winSize.height*0.5 ) );
        this._updateTowerFlag.setVisible(false);
        this._updateTowerFlag.setName( "c0.png"  );
        this.addChild(this._updateTowerFlag);

        filename = MainResource( "update0.png" );
        this._updateT = new cc.MenuItemImage(filename,filename,this.onTowerUpdate,this);
        this._updateT.setName( "update0.png"  );
        filename = MainResource( "remove.png" );
        this._removeT = new cc.MenuItemImage(filename,filename,this.onTowerRemove,this);
        filename = MainResource("num1.png");
        var xOff = 18,yOff = 1;
        this._updateLabel = cc.LabelAtlas.create("0", filename,10, 13, '0');
        this._updateLabel.setPosition( cc.p( xOff, yOff ) );
        this._updateT.addChild(this._updateLabel);
        this._removeLabel = cc.LabelAtlas.create("0", filename,10, 13, '0');
        this._removeLabel.setPosition( cc.p( xOff, yOff ) );
        this._removeT.addChild(this._removeLabel);

        var menu = cc.Menu.create( this._updateT,this._removeT );
        menu.setPosition( cc.p(0,0) );
        this.addChild( menu );

        this._updateT.setPosition( cc.p(-100,-100) );
        this._removeT.setPosition( cc.p(-100,-100) );

        //////////////////////////////////////////
        this._towerUpdate = new cc.Sprite();
        this.addChild(this._towerUpdate);
    },
    /////////////////////////
    isShow : function(){
        return this._isShow;
    },
    ///////////////////////////////////////////
    setUpdateMoney : function(money){
        this._updateLabel.setString(money);
    },
    setRemoveMoney : function(money){
        this._removeLabel.setString(money);
    },
    //////////////////////////////////////////////
    setUpdateTower : function(tower){
        this._updateTowerFlag.setUserData(tower);
    },
    getUpdateTower : function(){
      return this._updateTowerFlag.getUserData();
    },
    setPanelPosition : function(pos){
        this._updateTowerFlag.setPosition(pos);
        this._updateTowerFlag.setVisible(true);
        var uPos = cc.pAdd( pos,cc.p(0,70)),line=140;
        if( uPos.y>cc.winSize.height-line  )uPos = cc.pAdd( uPos,cc.p(65,-70) );
        this._updateT.setPosition( uPos );
        uPos = cc.pAdd( pos,cc.p(0,-70) )
        if( uPos.y<line  )uPos = cc.pAdd( uPos,cc.p(65,70) );
        this._removeT.setPosition( uPos );
        this._updateT.setVisible(true);
        this._removeT.setVisible(true);
    },
    alterTower : function(show,tower){
        if(show){
            var spr=tower.getNormalImage();
            if(spr){
                var frame = spr.displayFrame(),pos=tower.getPosition();
                this._towerUpdate.setSpriteFrame(frame);
                this._towerUpdate.setPosition(pos);
                this._towerUpdate.setVisible(true);
            }else{
                cc.log("invalid object from tower ");
            }
        }else{
            this._towerUpdate.setVisible(false);
        }
        cc.log("setting frame ");
    },
    //////////////////
    hidePanel : function(){
        this._updateTowerFlag.setVisible(false);
        this._updateT.setVisible(false);
        this._removeT.setVisible(false);
    },
    //////////////////////////////////////////////
    showPanel : function(show,tower){
        if(show){
            ////////////set other things//////////////////////
            this.setUpdateTower(tower);
            /////////////////////////
            var iu = getUILayerInstance();
            this.refreshTarget( iu.getMoney() );
            ////////////set if enable to update///////////////
            var data = tower.getUserData();
            var series = tower.getSeries();
            if( series < data.su.length ) {
                this.alterSkin(true);
                this.alterRegion(series);
                /////////////money////////////
                this.setUpdateMoney(data.su[series]);
                var price=tower.getPrice();
                this.setRemoveMoney(price*data.disc);
            }else{
                this.alterSkin(false);
                this.alterRegion(series);
                /////////////money////////////
                var price=tower.getPrice();
                this.setRemoveMoney(price*data.disc);
            }
            /////////////////////////////////////
            var pos = tower.getPosition();

            this.setPanelPosition(pos);
            this.alterTower(true,tower);

            this._isShow = true;
        }else{
            this.hidePanel();
            this.alterTower(false);
            this._isShow = false;
        }
    },
    alterRegion : function(series){
        switch(series){
            case 0:{
                var name = this._updateTowerFlag.getName( );
                if( name!="c0.png" ){
                    var filename = MainResource( "c0.png" );
                    var frame = new cc.SpriteFrame( filename,cc.rect(0,0,200,200) );
                    this._updateTowerFlag.setSpriteFrame(frame);
                    this._updateTowerFlag.setName( "c0.png"  );
                    cc.log("set region flag to 0 level");
                }else{
                    cc.log("no need to alter region flag ");
                }
                cc.log("show one 0 region now");
                break;
            }
            case 1:{
                var name = this._updateTowerFlag.getName( );
                if( name!="c0.png" ){
                    var filename = MainResource( "c0.png" );
                    var frame = new cc.SpriteFrame( filename,cc.rect(0,0,200,200) );
                    this._updateTowerFlag.setSpriteFrame(frame);
                    this._updateTowerFlag.setName( "c0.png"  );
                    cc.log("set region flag to 0 level");
                }else{
                    cc.log("no need to alter region flag ");
                }
                cc.log("show one 0 region now");
                break;
            }
            case 2:{
                var name = this._updateTowerFlag.getName( );
                if( name!="c1.png" ){
                    var filename = MainResource( "c1.png" );
                    var frame = new cc.SpriteFrame( filename,cc.rect(0,0,240,240) );
                    this._updateTowerFlag.setSpriteFrame(frame);
                    this._updateTowerFlag.setName( "c1.png"  );
                    cc.log("set region flag to 1 level");
                }else{
                    cc.log("no need to alter region flag ");
                }
                cc.log("show one 1 region now");
                break;
            }
            case 3:{
                var name = this._updateTowerFlag.getName( );
                if( name!="c2.png" ){
                    var filename = MainResource( "c2.png" );
                    var frame = new cc.SpriteFrame( filename,cc.rect(0,0,280,280) );
                    this._updateTowerFlag.setSpriteFrame(frame);
                    this._updateTowerFlag.setName( "c2.png"  );
                    cc.log("set region flag to 2 level");
                }else{
                    cc.log("no need to alter region flag ");
                }
                cc.log("show one 2 region now");
                break;
            }
            default : {
                cc.log("this is the max region now");
            }
        }
    },
    alterSkin : function( enable ){
        if( enable ){
            //this._updateT.setEnabled( true );
            var name = this._updateT.getName( );
            if( name!="update0.png" ){
                var filename = MainResource( "update0.png" );
                var frame = new cc.SpriteFrame( filename,cc.rect(0,0,65,52) );
                this._updateT.setNormalSpriteFrame(frame);
                this._updateT.setSelectedSpriteFrame(frame);
                this._updateT.setDisabledSpriteFrame(frame);
                this._updateT.setName( "update0.png"  );

                this._updateLabel.setVisible(true);
                cc.log("set enable down");
            }else{
                cc.log("no need to set enable ");
            }

        }else{
            //this._updateT.setEnabled( false );
            var name = this._updateT.getName( );
            if( name!="update1.png" ){
                var filename = MainResource( "update1.png" );
                var frame = new cc.SpriteFrame( filename,cc.rect(0,0,65,52) );
                this._updateT.setNormalSpriteFrame(frame);
                this._updateT.setSelectedSpriteFrame(frame);
                this._updateT.setDisabledSpriteFrame(frame);
                this._updateT.setName( "update1.png"  );

                this._updateLabel.setVisible(false);
                cc.log("set disable down");
            }else{
                cc.log("no need to set disable ");
            }
        }
    },
    //////////////////////////////////////////////
    onTowerUpdate : function(sender){
        cc.log("on update");
        // update series
        var tower=this.getUpdateTower();
        var data = tower.getUserData();
        var series = tower.getSeries();
        if( series < data.su.length ){
            var iu = getUILayerInstance();
            var spend = data.su[series]
            if ( iu.getMoney()>= spend ) {
                iu.addMoney(-spend);
                tower.addPrice(spend);
                tower.updateSeries();
                this.showPanel(false);

                var filename = AudioResource("update.mp3");
                cc.audioEngine.playEffect(filename);
            }else{
                cc.log("no money to update now");
            }

        }else{
            cc.log("can not update now");
        }
    },
    onTowerRemove : function(sender){
        cc.log("on remove");
        var tower=this.getUpdateTower();
        // add money back to game
        var price=tower.getPrice();
        var data = tower.getUserData();
        var iu = getUILayerInstance();
        iu.addMoney( price*data.disc );
        // remove it
        tower.removeFromParent();
        // un register
        Unregister1(tower.getPoint());
        // invisible
        this.showPanel(false);
    },
    // refresh enable towers
    refreshTarget : function(gold){
        var name = this._updateT.getName( );
        if( name=="update0.png" ){
            var tower=this.getUpdateTower();
            if( tower ){
                var data = tower.getUserData();
                var series = tower.getSeries();
                var cost=data.su[series];
                if ( gold < cost){
                    this.setTowerEnable(this._updateT,false);
                }else{
                    this.setTowerEnable(this._updateT,true);
                }
            }else{
                cc.log("update tower is null");
            }
        }
    },
    // setting tower enable or not
    setTowerEnable : function(target,enable){
        if(enable){
            target.setColor(cc.color.WHITE);
        }else{
            target.setColor(cc.color(135,123,103));
        }
    }
});
RefreshTarget=function(gold){
    g_updateProxy.refreshTarget(gold);
    cc.log("refresh target");
}
ShowUpdatePanel=function(tower){
    var show = g_updateProxy.isShow();
    cc.log("show update panel: "+!show);
    g_updateProxy.showPanel(!show,tower);

    //////////////////////////////////////////
    g_placeProxy.showPanel(false);
}
